<script>
// 任务二 答题页面
export default {
  name: "TxTwoComm",
  data(){
    return {
      show_comp:false,
      question:'',
      show_plus:false,
      show_que1:false,
      show_que2:false,
      show_blank:false,
      start_time:'',
      end_time:'',
      first:'',
      second:'',
      third:'',
      fourth:''
    }
  },

  methods:{
    //显示答题组件
    show_comm(question){
      this.question = question;
      //拆解词语，变成单字，便于留白
      this.quesiont_split()
      //显示整个答题组件
      this.show_comp = true;
      //显示红色+号
      this.show_plus = true;

      //红色+号300ms后隐藏
      this.hide_btn();
    },

    //初始化组件时，拆解词组
    quesiont_split(){
      var letr1 = this.question.textQuestion.split("");
      var letr2 = this.question.sameQuestion.split("");
      if(letr1[0]){
        this.first = letr1[0]
      }
      if(letr1[1]){
        this.second = letr1[1]
      }
      if(letr2[0]){
        this.third = letr2[0]
      }
      if(letr2[1]){
        this.fourth = letr2[1]
      }

    },


    // +号按钮300毫秒后消失
    hide_btn(){
      const _this = this;
      this.$nextTick(function(){
        setTimeout(function(){
          _this.show_plus = false;
          //显示相似词一  2秒种
          _this.showQue1();
        },300)
      })
    },

    //显示相似提示词2秒，然后显示相似词，并开始作答计时
    // 暂时修改为2000----》20
    showQue1(){
        const _this = this;
        _this.show_que1 = true;
        _this.$nextTick(function() {
          setTimeout(function () {
            _this.show_que1 = false;
            _this.show_que2 = true;
            _this.start_time = new Date().getTime();
          }, 2000)
        })
    },


    //用户选择答案，并统计用户答题时长
    choose_answer(ans){
      //统计结束时间
      this.end_time = new Date().getTime();

      var spend_time = Math.abs(this.end_time - this.start_time);
      //统计答案是否正确
      var is_right = '是';
      if(this.question.rightAnswer != ans){
        is_right = '否';
      }

      //显示空屏
      this.show_que2 = false;
      this.show_blank = true;

      //300毫秒后，空屏隐藏，进入下一题
      const _this = this;
      this.$nextTick(function(){
        setTimeout(function(){
          _this.clear_data();
          // console.log(spend_time);
          _this.$emit('ans_finish',ans,spend_time,is_right); //空屏显示300ms之后，回调父函数，传给父元素答案，并告知父元素进入下一题
        },300)
      })
    },

    //答题完成---清空答题组件
    clear_data(){
      this.show_comp = false;
      this.question='';
      this.show_plus= false;
      this.show_que1=false;
      this.show_que2=false;
      this.show_blank=false;
    }
  }
}
</script>

<template>
<!-- 答题组件，平时隐藏，用户调用时显示 -->
  <div v-if="show_comp" style="width: 50%;">
<!-----------红色的+号，显示300ms后隐藏------------>
    <div v-if="show_plus" class="card1_plus">
      <div  ref="btn1" class="card1_btn1" >+</div>
    </div>

<!-----------相似性提示词，显示2秒后隐藏------------>
    <div v-if="show_que1" class="que_sty">
      <span v-if="first!=''">{{first}}</span>
      <span v-if="second!=''">&nbsp;{{second}}</span>
    </div>

<!-----------相似性问题，用户作答后隐藏------------>
    <div v-if="show_que2" class="card1">
      <div class="que_sty2">
        <!--                <span>{{question.textQuestion}}</span>-->
        <span v-if="third!=''">{{third}}</span>
        <span v-if="fourth!=''">&nbsp;{{fourth}}</span>
      </div>
      <div class="div_btn1">
        <div>
          <el-button class="btn2" type="primary" @click="choose_answer('是')">是</el-button>
          <p style="font-size: 40px;margin-top: 20px">(是近义词)</p>
        </div>
        <div>
          <el-button class="btn2" type="primary" @click="choose_answer('否')">否</el-button>
          <p style="font-size: 40px;margin-top: 20px">(不是近义词)</p>
        </div>
      </div>
    </div>


<!------------空屏显示300毫秒后隐藏------------->
    <div v-if="show_blank" class="card1"></div>
  </div>
</template>

<style scoped>
.card1{
  text-align: center;
  width: 100%;
}
.card1_btn1{
  width: 200px;
  height: 200px;
  line-height: 170px;
  font-size: 250px;
  color:red;
}
.card1_plus{
  text-align: center;
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 25%;
}

.que_sty{
  color: #050505;
  font-size: 160px;
  font-weight: 700;
  margin-top: -200px;
  text-align: center;
}

.que_sty2{
  color: #050505;
  font-size: 160px;
  font-weight: 700;
  margin-top: -40px;
}

.sub_title{
  font-size: 50px;
}

.div_btn1{
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}

.btn2{
  width: 187px;
  height: 90px;
  font-size: 60px;
  display: inline-block;
}

</style>